#coding:utf-8

import wx
import cPickle as pickle

class SandBoxPanel(wx.Panel):
	
	def __init__(self,parent,size=(300,300)):
		wx.Panel.__init__(self,parent,-1,size=size)
		
		self.Bind(wx.EVT_MOTION,self.OnMotion)		
		self.Bind(wx.EVT_LEFT_UP ,self.OnLeftUp)		
		self.Bind(wx.EVT_LEFT_DOWN ,self.OnLeftDown)	
		
	def OnMotion(self,evt):
		for c in self.GetChildren():
			c._absPos = evt.GetPosition()
			c.doAction(evt.GetPosition())				

	def OnLeftDown(self,evt):
		for c in self.GetChildren():
			c._absPos = evt.GetPosition()
			
	def OnLeftUp(self,evt):
		for c in self.GetChildren():			
			c.resetState(128)
			

class PanelSource(wx.Panel):
	def __init__(self,parent,obj_context,obj_str,size=(40,40)):
		wx.Panel.__init__(self,parent,-1,size=size,style=wx.SIMPLE_BORDER)
		
		data = pickle.dumps(obj_context)
		self.source = wx.CustomDataObject(obj_str)		
		self.source.SetData(data)			
		
		self.SetMinSize(size)
		
		self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
		
	
	def OnLeftDown(self,evt):
		dropSource = wx.DropSource(self)
		dropSource.SetData(self.source)
		
		result = dropSource.DoDragDrop(wx.Drag_AllowMove)
		if result == wx.DragMove:
			print "FIM DROP"


class PanelTarget(wx.DropTarget):
	def __init__(self,window,custom_obj_str):
		wx.DropTarget.__init__(self)
		
		self.window = window
		self.data = wx.CustomDataObject(custom_obj_str)
		self.SetDataObject(self.data)	
		
	def OnData(self,x,y,res):
		self.GetData()
		
		drop_object_str = self.data.GetData()		
		drop_object = pickle.loads(drop_object_str)
		
		drop_object.create( self.window,(x,y) )
		
		return res
		